package servlet;

import it.polimi.swimv2.entities.User;
import it.polimi.swimv2.remote.HelpSearchManagerRemote;
import it.polimi.swimv2.remote.UserSearchManagerRemote;
import it.polimi.swimv2.util.RemoteBindingName;

import java.io.IOException;
import java.util.Collection;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class UserSearchServlet
 */
public class UserSearchServlet extends HttpServlet {
	
	//Parameters in HTTP Request
	private static final String CITY = "city";
	private static final String SURNAME = "surname";
	private static final String FIRSTNAME = "firstname";
	private static final String USERNAME = "username";
	private static final String SEARCH_RESULT = "searchResult";
	private static final String SEARCH_FOR_USER = "searchForUser";
	private static final String SEARCH_FOR_USER_FORM = "searchForUserForm";
	private static final String ACT = "act";
	
	//Jsp Pages
	private static final String ERROR_JSP = "error.jsp";
	private static final String SEARCH_RESULT_JSP = "searchResult.jsp";	
	private static final String INDEX_JSP = "index.jsp";
	private static final String SEARCH_FOR_USER_JSP = "searchForUser.jsp";

	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserSearchServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		chooseAction(request, response);
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		chooseAction(request, response);
	}
	
	/**
	 * manages the action requested by http request
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void chooseAction(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String act = request.getParameter(ACT);
			if(act.equals(SEARCH_FOR_USER_FORM)){
				response.sendRedirect(SEARCH_FOR_USER_JSP);
			}else if(act.equals(SEARCH_FOR_USER)){
				searchForUser(request, response);
			}else{
				response.sendRedirect(INDEX_JSP);
			}
	}
	/**
	 * manages the search and shows the results list
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void searchForUser(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		
			try{
				UserSearchManagerRemote rem = retrieveUserSearchManagerRemote();
				Collection<User> foundUsers = manageSearchRequest(request, rem);
				request.setAttribute(SEARCH_RESULT, foundUsers);
				RequestDispatcher dispatcher = request.getRequestDispatcher(SEARCH_RESULT_JSP);
				dispatcher.forward(request, response); 
			}catch(NamingException e){
				response.sendRedirect(ERROR_JSP);
			}
	}
	/**
	 * retrieves the results list
	 * @param request
	 * @param rem
	 * @return
	 */
	private Collection<User> manageSearchRequest(HttpServletRequest request, UserSearchManagerRemote rem) {
		String username = request.getParameter(USERNAME);
		String firstname = request.getParameter(FIRSTNAME);
		String surname = request.getParameter(SURNAME);
		String city = request.getParameter(CITY);
		return rem.userSearch(username, firstname, surname, city);
	}
	/**
	 * 
	 * @return
	 * @throws NamingException
	 */
	private UserSearchManagerRemote retrieveUserSearchManagerRemote() throws NamingException {
		Context jndiContext = new InitialContext();
		Object ref = jndiContext.lookup(RemoteBindingName.USER_SEARCH_MANAGER);
		return (UserSearchManagerRemote) ref;
	}

}
